package cn.iocoder.yudao.module.cfs.controller.admin.userappconfig;

import cn.iocoder.yudao.module.cfs.controller.admin.userappconfig.vo.UserAppConfigPageReqVO;
import cn.iocoder.yudao.module.cfs.controller.admin.userappconfig.vo.UserAppConfigRespVO;
import cn.iocoder.yudao.module.cfs.controller.admin.userappconfig.vo.UserAppConfigSaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.cfs.dal.dataobject.userappconfig.UserAppConfigDO;
import cn.iocoder.yudao.module.cfs.service.userappconfig.UserAppConfigService;

@Tag(name = "管理后台 - 社会运营平台_app配置")
@RestController
@RequestMapping("/cfs/user-app-config")
@Validated
public class UserAppConfigController {

    @Resource
    private UserAppConfigService userAppConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建社会运营平台_app配置")
    @PreAuthorize("@ss.hasPermission('cfs:user-app-config:create')")
    public CommonResult<Long> createUserAppConfig(@Valid @RequestBody UserAppConfigSaveReqVO createReqVO) {
        return success(userAppConfigService.createUserAppConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新社会运营平台_app配置")
    @PreAuthorize("@ss.hasPermission('cfs:user-app-config:update')")
    public CommonResult<Boolean> updateUserAppConfig(@Valid @RequestBody UserAppConfigSaveReqVO updateReqVO) {
        userAppConfigService.updateUserAppConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除社会运营平台_app配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cfs:user-app-config:delete')")
    public CommonResult<Boolean> deleteUserAppConfig(@RequestParam("id") Long id) {
        userAppConfigService.deleteUserAppConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得社会运营平台_app配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cfs:user-app-config:query')")
    public CommonResult<UserAppConfigRespVO> getUserAppConfig(@RequestParam("id") Long id) {
        UserAppConfigDO userAppConfig = userAppConfigService.getUserAppConfig(id);
        return success(BeanUtils.toBean(userAppConfig, UserAppConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得社会运营平台_app配置分页")
    @PreAuthorize("@ss.hasPermission('cfs:user-app-config:query')")
    public CommonResult<PageResult<UserAppConfigRespVO>> getUserAppConfigPage(@Valid UserAppConfigPageReqVO pageReqVO) {
        PageResult<UserAppConfigDO> pageResult = userAppConfigService.getUserAppConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserAppConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出社会运营平台_app配置 Excel")
    @PreAuthorize("@ss.hasPermission('cfs:user-app-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserAppConfigExcel(@Valid UserAppConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserAppConfigDO> list = userAppConfigService.getUserAppConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "社会运营平台_app配置.xls", "数据", UserAppConfigRespVO.class,
                        BeanUtils.toBean(list, UserAppConfigRespVO.class));
    }

}